package com.jwaoo.chat.session;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.view.View;
import com.jwaoo.chat.ChatHelpter;
import com.jwaoo.chat.Message;
import com.jwaoo.chat.R;
import com.jwaoo.chat.SessionCustomization;
import com.jwaoo.chat.attachment.MsgAttachment;
import com.jwaoo.chat.extension.CustomAttachment;
import com.jwaoo.chat.extension.StickerAttachment;
import com.jwaoo.chat.session.actions.BaseAction;
import com.jwaoo.chat.viewholder.MsgViewHolderDefCustom;
import java.util.ArrayList;

public class SessionHelper {

    private static final int ACTION_HISTORY_QUERY = 0;
    private static final int ACTION_SEARCH_MESSAGE = 1;
    private static final int ACTION_CLEAR_MESSAGE = 2;

    private static SessionCustomization p2pCustomization;
    private static SessionCustomization teamCustomization;
    private static SessionCustomization myP2pCustomization;

    public static void init() {

        // 设置会话中点击事件响应处理
        setSessionListener();

        // 注册消息转发过滤器
//        registerMsgForwardFilter();

        // 注册消息撤回过滤器
        registerMsgRevokeFilter();

        // 注册消息撤回监听器
        registerMsgRevokeObserver();

        ChatHelpter.setCommonP2PSessionCustomization(getP2pCustomization());
    }

    public static void startP2PSession(Context context, String account) {
        startP2PSession(context, account, null);
    }

    public static void startP2PSession(Context context, String account, Message anchor) {
//        if (!JWCache.getAccount().equals(account)) {
        if (true) {
//            ChatHelpter.startP2PSession(context, account, anchor);
        }
//        else {
//            ChatHelpter.startChatting(context, account, SessionTypeEnum.P2P, getMyP2pCustomization(), anchor);
//        }
    }

    // 定制化单聊界面。如果使用默认界面，返回null即可
    private static SessionCustomization getP2pCustomization() {
        if (p2pCustomization == null) {
            p2pCustomization = new SessionCustomization() {
                // 由于需要Activity Result， 所以重载该函数。
                @Override
                public void onActivityResult(final Activity activity, int requestCode, int resultCode, Intent data) {
                    super.onActivityResult(activity, requestCode, resultCode, data);

                }

                @Override
                public MsgAttachment createStickerAttachment(String category, String item) {
                    return new StickerAttachment(category, item);
                }
            };

            // 背景
//            p2pCustomization.backgroundColor = Color.BLUE;
//            p2pCustomization.backgroundUri = "file:///android_asset/xx/bk.jpg";
//            p2pCustomization.backgroundUri = "file:///sdcard/Pictures/bk.png";
//            p2pCustomization.backgroundUri = "android.resource://com.netease.nim.demo/drawable/bk"

            // 定制加号点开后可以包含的操作， 默认已经有图片，视频等消息了
            ArrayList<BaseAction> actions = new ArrayList<>();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
//                actions.add(new AVChatAction(AVChatType.AUDIO));
//                AUDIOactions.add(new AVChatAction(AVChatType.VIDEO));
            }
//            actions.add(new RTSAction());
//            actions.add(new SnapChatAction());
//            actions.add(new GuessAction());
//            actions.add(new FileAction());
//            actions.add(new TipAction());
            p2pCustomization.actions = actions;
            p2pCustomization.withSticker = true;

            // 定制ActionBar右边的按钮，可以加多个
            ArrayList<SessionCustomization.OptionsButton> buttons = new ArrayList<>();
            SessionCustomization.OptionsButton cloudMsgButton = new SessionCustomization.OptionsButton() {
                @Override
                public void onClick(Context context, View view, String sessionId) {
//                    initPopuptWindow(context, view, sessionId, SessionTypeEnum.P2P);
                }
            };
            cloudMsgButton.iconId = R.drawable.nim_ic_messge_history;

            SessionCustomization.OptionsButton infoButton = new SessionCustomization.OptionsButton() {
                @Override
                public void onClick(Context context, View view, String sessionId) {
//                    MessageInfoActivity.startActivity(context, sessionId); //打开聊天信息
                }
            };

            infoButton.iconId = R.drawable.nim_ic_message_actionbar_p2p_add;

            buttons.add(cloudMsgButton);
            buttons.add(infoButton);
            p2pCustomization.buttons = buttons;
        }

        return p2pCustomization;
    }

    private static void setSessionListener() {
        SessionEventListener listener = new SessionEventListener() {
            @Override
            public void onAvatarClicked(Context context, Message message) {
                // 一般用于打开用户资料页面
//                UserProfileActivity.start(context, message.getFromAccount());
            }

            @Override
            public void onAvatarLongClicked(Context context, Message message) {
                // 一般用于群组@功能，或者弹出菜单，做拉黑，加好友等功能
            }
        };

        ChatHelpter.setSessionListener(listener);
    }

    /**
     * 消息撤回过滤器
     */
    private static void registerMsgRevokeFilter() {
//        NimUIKit.setMsgRevokeFilter(new MsgRevokeFilter() {
//            @Override
//            public boolean shouldIgnore(IMMessage message) {
//                if (message.getAttachment() != null
//                        && (message.getAttachment() instanceof AVChatAttachment)) {
//                    // 视频通话消息和白板消息 不允许撤回
//                    return true;
//                } else if (JWCache.getAccount().equals(message.getSessionId())) {
//                    // 发给我的电脑 不允许撤回
//                    return true;
//                }
//                return false;
//            }
//        });
    }

    private static void registerMsgRevokeObserver() {
//        NIMClient.getService(MsgServiceObserve.class).observeRevokeMessage(new Observer<IMMessage>() {
//            @Override
//            public void onEvent(IMMessage message) {
//                if (message == null) {
//                    return;
//                }
//
//                MessageHelper.getInstance().onRevokeMessage(message);
//            }
//        }, true);
    }


}
